package com.yuanguangsheng.disk.file.control;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuanguangsheng.disk.file.entity.SearchFileEntity;
import com.yuanguangsheng.disk.file.service.FileIoService;
import com.yuanguangsheng.disk.file.service.FileService;
import com.yuanguangsheng.disk.mybatis.entity.PtFileEntity;
import com.yuanguangsheng.disk.mybatis.flexigrid.PagingEntity;
import com.yuanguangsheng.disk.mybatis.flexigrid.TblResponseEntity;
import com.yuanguangsheng.disk.role.entity.SearchRoleEntity;
import com.yuanguangsheng.disk.spring.control.BaseController;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
    @Resource
    private FileService fileService;

    @Resource
    private FileIoService fileIoService;

    @RequestMapping("/fileAll")
    public String fileAll(){
        return "file/filePage";
    }
    @RequestMapping("/syncFileData")
    @ResponseBody
    public String syncFileData(){
        fileIoService.syncFileData();
        return successJson("file.sync.data.success");
    }
    @RequestMapping("/queryFileInfoListByPage")
    @ResponseBody
    public String queryFileInfoListByPage(SearchFileEntity entity, PagingEntity paging){

        TblResponseEntity<PtFileEntity> tblResponseEntity = fileService.queryFileInfoListByPage(entity,paging);
        return JSON.toJSONString(tblResponseEntity);
    }


    @RequestMapping("/queryFileInfoById")
    @ResponseBody
    public String queryFileInfoById(@RequestParam("fileUuid") String fileUuid){

        PtFileEntity ptFileEntity = fileService.queryFileInfoById(fileUuid);
        return JSON.toJSONString(ptFileEntity);
    }

    @RequestMapping("/mkdirFileInfo")
    @ResponseBody
    public String mkdirFileInfo(@RequestParam("parentId") String parentId,@RequestParam("folderName") String folderName){

        fileIoService.mkdirFileInfo(parentId,folderName);
        return successJson("file.mkdir.success");
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(MultipartHttpServletRequest request,@RequestParam("parentId") String parentId) throws IOException {
        MultipartFile multipartFile = request.getFile("fileName");
        String fileName = multipartFile.getOriginalFilename();

        fileIoService.uploadFile(parentId,fileName,multipartFile.getInputStream());

        return successJson("file.upload.success");
    }


    @RequestMapping("/copyFile")
    @ResponseBody
    public String copyFile(@RequestParam("parentId") String parentId,@RequestParam("srcUuidList") String srcUuidList) {
        fileIoService.copyFile(parentId,srcUuidList);
        return successJson("file.copy.success");
    }
    @RequestMapping("/downFile/{fileUuid}")
    @ResponseBody
    public ResponseEntity<byte[]> downFile(@PathVariable("fileUuid") String fileUuid){
        ResponseEntity<byte[]> responseEntity = fileIoService.downFile(fileUuid);
        return responseEntity;
    }
    @RequestMapping("/getShareUrl")
    @ResponseBody
    public String getShareUrl(@RequestParam("fileUuid") String fileUuid){
        String url = fileIoService.getShareUrl(fileUuid);
        return successJson("file.share.success",url);
    }

    @RequestMapping("/queryShareUrl")
    @ResponseBody
    public String queryShareUrl(@RequestParam("fileUuid") String fileUuid,@RequestParam("fileValidity") Integer fileValidity){
        String url = fileIoService.queryShareUrl(fileUuid,fileValidity);
        return successJson("file.share.success",url);
    }



}